package ua.kpi_java_training.zabrodskyi.module1;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

/**
 * Class is designed to test methods of the class PalindromeFinder.
 * 
 * @version 1.03 25 May 2013
 * @author Maksym Zabrodskyi
 */
public class PalindromeFinderTest {

	/**
	 * The test of a method for finding palindromes. From the phrase-resource
	 * selects only the substring-palindromes, a new line is created from
	 * substrings-palindromes.
	 */
	@Test
	public void testFindPalindromes() {
		String phrase = "abba, TFT, abc, radar";
		String expectedListPalindromes = "abba\nTFT\nradar\nada\n";
		String actualListPalindromes = PalindromeFinder.findPalindromes(phrase);
		Assert.assertEquals(expectedListPalindromes, actualListPalindromes);
	}

	/**
	 * The test of a method which to the existing list of palindromes adds only
	 * a new palindrome.
	 */
	@Test
	public void testAddOnlyPalindrome() {
		String listPalindromes = "abba\nTFT\n";
		String subPhrase = "radar";
		String expectedList = listPalindromes + subPhrase + "\n";
		String actualList = PalindromeFinder.addOnlyPalindrome(listPalindromes,
				subPhrase);
		Assert.assertEquals(expectedList, actualList);
	}

	/**
	 * The test of a method which to the existing list of palindromes adds only
	 * a new palindrome. If substring isn't a palindrome the list of palindromes
	 * doesn't change.
	 */
	@Test
	public void testNotAddIfNotPalindrome() {
		String listPalindromes = "abba\nTFT\n";
		String subPhrase = "radak";
		String expectedList = listPalindromes;
		String actualList = PalindromeFinder.addOnlyPalindrome(listPalindromes,
				subPhrase);
		Assert.assertEquals(expectedList, actualList);
	}

	/**
	 * The test of a method which checks there is a word a palindrome. True if a
	 * word is a palindrome.
	 */
	@Test
	public void testIsPalindrome() {
		String word = "radar";
		assertTrue(PalindromeFinder.isPalindrome(word));
	}

	/**
	 * The test of a method which checks there is a word a palindrome. False if
	 * a word is not a palindrome.
	 */
	@Test
	public void testIsNotPalindrome() {
		String word = "radak";
		assertFalse(PalindromeFinder.isPalindrome(word));
	}

	/**
	 * The test method that checks the input phrase is correct. The phrase is
	 * considered correct if the length of an input the phrase is equal to or
	 * over than {@link PalindromeFinder#MIN_PALINDROME_LENGTH minimum length of
	 * a palindrome.}
	 */
	@Test
	public void testIsPhraseCorrect() {
		String inputPhrase = "abc";
		assertTrue(PalindromeFinder.isPhraseCorrect(inputPhrase));
		inputPhrase = "abba, radak";
		assertTrue(PalindromeFinder.isPhraseCorrect(inputPhrase));
	}

	/**
	 * The test method that checks the input phrase is correct. The phrase is not
	 * correct if the length of an input phrase is less than
	 * {@link PalindromeFinder#MIN_PALINDROME_LENGTH minimum length of a
	 * palindrome.}
	 */
	@Test
	public void testIsNotPhraseCorrect() {
		String inputPhrase = "ra";
		assertFalse(PalindromeFinder.isPhraseCorrect(inputPhrase));
	}

}
